﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Bids.Core.Common.Data
{
    public static class DataExtend
    {
        /// <summary>
        /// 将数据转换成int32型
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns>int.MinValue</returns>
        public static int ToInt32(this object parameters)
        {
            try
            {
                return Convert.ToInt32(parameters);
            }
            catch (Exception e)
            {
                return int.MinValue;
            }
        }

        /// <summary>
        /// 将数据转换成DataTime型
        /// </summary>
        /// <param name="parameters">DataTime.MinValue</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object parameters)
        {
            try
            {
                return Convert.ToDateTime(parameters);
            }
            catch (Exception e)
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// 将数据转换成Decimal型
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static Decimal ToDecimal(this object parameters)
        {
            try
            {
                return Convert.ToDecimal(parameters);
            }
            catch (Exception e)
            {
                return 0;
            }
        }

        /// <summary>
        /// 将数据转换成int64型
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns>long.MinValue</returns>
        public static long ToInt64(this object parameters)
        {
            try
            {
                return Convert.ToInt64(parameters);
            }
            catch (Exception e)
            {
                return long.MinValue;
            }
        }

        public static double ToDouble(this object value)
        {
            try
            {
                return Convert.ToDouble(value);
            }
            catch (Exception e)
            {
                return double.MinValue;
            }
        }

        /// <summary>
        /// 转换成byte
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte ToByte(this object obj)
        {
            try
            {
                return Convert.ToByte(obj);
            }
            catch { return byte.MinValue; }
        }
    }
}
